﻿
function ApproveCancelOrderRequest(id,typeId, actionId, reason,orderref, orderitemid) {
    if (!confirm("You are sure you want to approve request?")) return;
    if (actionId == 0) {
        alert("Please select action!");
        return;
    }
    if (typeId == 5 && actionId == 1) CancelItemAction(id, actionId, reason, orderref, orderitemid);
    else if (typeId == 1) CreateRDMOrder(id, actionId, orderref,reason);
    else ApproveAction(id, actionId, reason);
}

function DeclineCancelOrderRequest(id, reason) {
    if (!confirm("You are sure you want to decline request?")) return;
    App.blockUI($('#FilterResults'));
    $.ajax({
        url: "/Customer/DeclineCancelRequest",
        method: "GET",
        data: { id: id, reason: reason },
        success: function (data) {
            if (data != "") {
                alert(data);
            }
            App.unblockUI($('#FilterResults'));
            $('#frSearchCancelOrderRequest').submit();
        }, error: function () {
            App.unblockUI($('#FilterResults'));
        }
    });
}
function ApproveAction(id, actionId,reason) {
    $.ajax({
        url: "/Customer/ApproveCancelRequest",
        method: "GET",
        data: { id: id, actionId: actionId, reason: reason },
        success: function (data) {
            $('#frSearchCancelOrderRequest').submit();
            App.unblockUI($('#FilterResults'));
        }, error: function () {
            App.unblockUI($('#FilterResults'));
            alert('Error!');
        }
    });
}
function CancelItemAction(id, actionId, reason, orderref, orderitemid) {
    if (orderitemid == 0) {
        CancelOrderAction(id, actionId, reason, orderref);
    } else {
        App.blockUI($('#FilterResults'));
        $.ajax({
            url: '/orderview/CheckOrderItemIsInLoad',
            data: { orderItemId: orderitemid },
            success: function(rs) {
                if (rs == "False") {
                    var child = PopupCenter('/orderview/cancelorderitem?orderitemid=' + orderitemid, 780, 500);
                    var timer = setInterval(checkChild, 500);

                    function checkChild() {
                        if (child != undefined && child.closed) {
                            clearInterval(timer);
                            App.unblockUI($('#FilterResults'));
                            $.ajax({
                                url: "/orderview/CheckOrderItemIsCancelled",
                                method: "GET",
                                data: { orderItemId: orderitemid },
                                success: function(isCancelled) {
                                    if (isCancelled == 'True') {
                                        ApproveAction(id, actionId, reason);
                                    }
                                },
                                error: function() {
                                    alert('Error!');
                                }
                            });
                        }
                    }

                } else {
                    alert("Please remove this order from the load first");
                    App.unblockUI($('#FilterResults'));
                }
            }
        });
    }
}
function CancelOrderAction(id, actionId, reason, orderref) {
    App.blockUI($('#FilterResults'));
    $.ajax({
        url: '/orderview/CheckOrderRefIsInLoad',
        data: { orderRef: orderref },
        success: function (rs) {
            if (rs == "") {
                var child = PopupCenter('/orderview/cancelorder?orderref=' + orderref, 780, 500);
                var timer = setInterval(checkChild, 500);

                function checkChild() {
                    if (child != undefined && child.closed) {
                        clearInterval(timer);
                        App.unblockUI($('#FilterResults'));
                        $.ajax({
                            url: "/orderview/CheckOrderIsCancelled",
                            method: "GET",
                            data: { orderRef: orderref },
                            success: function (isCancelled) {
                                if (isCancelled == 'True') {
                                    ApproveAction(id, actionId, reason);
                                }
                            },
                            error: function () {
                                alert('Error!');
                            }
                        });
                    }
                }
            } else {
                alert(rs);
                App.unblockUI($('#FilterResults'));
            }
        }
    });
}

function CreateRDMOrder(id, actionId, orderRef, reason) {
    App.blockUI($('#FilterResults'));
    var child = window.open("/Customer/CreateRdmOrderFromRequest?id=" + id + '&actionid=' + actionId, "_blank");
    var timer = setInterval(checkChild, 500);
    function checkChild() {
        if (child != undefined && child.closed) {
            clearInterval(timer);
            ApproveAction(id, actionId, reason);
        }
    }
}